<?php
namespace app\service;
use think\facade\Db;

class waterService{
    //多条件查询
    public function waterWhereList($community_id,$page,$where){
        return Db::name('water')->where('community_id', 'in', $community_id)->where($where)->order('id desc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
    }
    public function waterWhereListNumber($community_id,$where){
        return Db::name('water')->where('community_id', 'in', $community_id)->where($where)->count();
    }
    //根基小区id查询小区名称
    public function communityName($community_id){
        return Db::name('community')->where('community_id',$community_id)->value('community_name');
    }
    //查询小区
    public function communityInfo($whe){
        return Db::name('community')->where($whe)->select();
    }
    //查询业主姓名
    public function ownerName($community_id,$group,$building,$unit,$room){
        return Db::name('roominfo')
                ->where('community_id',$community_id)
                ->where('group',$group)
                ->where('building',$building)
                ->where('unit',$unit)
                ->where('room',$room)
                ->value('owner_name');
    }
    //查询业主上期吨数
    public function upperStage($community_id,$group,$building,$unit,$room,$owner_name){
        return Db::name('water')
                ->where('community_id',$community_id)
                ->where('group',$group)
                ->where('building',$building)
                ->where('unit',$unit)
                ->where('room',$room)
                ->where('owner_name',$owner_name)->value('book_stage');
    }
    //查询水费单价
    public function unitPrice($community_id){
        return Db::name('community')->where('community_id',$community_id)->value('water');
    }
    //查询电费单价
    public function electricUnitPrice($community_id){
        return Db::name('community')->where('community_id',$community_id)->value('electric');
    }
    //添加水费数据
    public function waterAdd($data){
        return Db::name('water')->insert($data);
    }
    //查询抄表员姓名
    public function meterReader($id){
        return Db::name('admin')->where('id',$id)->value('name');
    }
    //删除水费记录
    public function waterId($id){
        return Db::name('water')->where('id',$id)->find();
    }
    //查询指定id是否存在
    public function waterListId($data){
        return Db::name('water')->where('id','in',$data)->select();
    }
    //查询房屋编号
    public function outroomid($community_id,$address){
        return Db::name('roominfo')->where('community_id',$community_id)->where('address',$address)->value('out_room_id');
    }
    //插入水费数据
    public function waterBillAdd($data){
        return Db::name('bill')->insert($data);
    }
    //查询是否已经存在数据
    public function waterCommunityAddress($community_id,$room_address){
        return Db::name('water')->where('community_id',$community_id)->where('room_address',$room_address)->find();
    }
    //修改电表数据
    public function waterEdit($info){
        return Db::name('water')->update($info);
    }
    //--------------------------------------------------------------------------------电费-------------------------------------------------------------------------------------------------//
    //查询电费
    public function electricInfo(){
        return Db::name('electric')->order('id desc')->paginate(100);
    }
    public function electricWhereList($community_id,$page,$where){
        return Db::name('electric')->where('community_id', 'in', $community_id)->where($where)->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
    }
    public function electricWhereListNumber($community_id,$where){
        return Db::name('electric')->where('community_id', 'in', $community_id)->where($where)->select();
    }
    //查询业主上期吨数
    public function electricUpperStage($community_id,$group,$building,$unit,$room,$owner_name){
        return Db::name('electric')
                ->where('community_id',$community_id)
                ->where('group',$group)
                ->where('building',$building)
                ->where('unit',$unit)
                ->where('room',$room)
                ->where('owner_name',$owner_name)
                ->value('book_stage');
    }
    //添加水费数据
    public function electricAdd($data){
        return Db::name('electric')->insert($data);
    }
    //查询是否已经存在数据
    public function electricrCommunityAddress($community,$room_address){
        return Db::name('electric')->where('community_id',$community)->where('room_address',$room_address)->find();
    }
    //修改电表数据
    public function electricEdit($info){
        return Db::name('electric')->update($info);
    }
    //查询是否存在数据
    public function electricListId($data){
        return Db::name('electric')->where('id','in',$data)->select();
    }
    //---------------------------------------------------------------------------------导入----------------------------------------------------------------------------------------//
    //查询小区名称
    public function communityNameId($communityName){
        return Db::name('community')->where('community_name', $communityName)->find();
    }
    //查询房屋是否存在
    public function roomAddressInfo($community_id,$room_address){
        return Db::name('roominfo')->where('community_id',$community_id)->where('address',$room_address)->where('status',1)->find();
    }
    //添加
    public function waterExcelAdd($data){
        return Db::name('water')->insertGetId($data);
    }
    //添加
    public function electricExcelAdd($data){
        return Db::name('electric')->insertGetId($data);
    }
    //---------------------------------------------------------------------------------导出----------------------------------------------------------------------------------------//
    //查询指定id是否存在
    public function waterEntryIdInfo($data){
        if (is_array($data)) {
            return Db::name('water')->where('id', 'in', $data)->select();
        }else{
            return Db::name('water')->where('id', $data)->find();
        }
    }
    //导出条件查询
    public function waterWhereExport($where){
        return Db::name('water')->where($where)->select();
    }
    //查询小区名称
    public function communityIdName($community_id){
        return Db::name('community')->where('community_id',$community_id)->value('community_name');
    }
    //查询指定id是否存在
    public function electricEntryIdInfo($data){
        if (is_array($data)) {
            return Db::name('electric')->where('id', 'in', $data)->select();
        }else{
            return Db::name('electric')->where('id', $data)->find();
        }
    }
    //导出条件查询
    public function electricWhereExport($where){
        return Db::name('electric')->where($where)->select();
    }
}